package com.data.web.controller.system;


import com.data.biz.domain.BizFan;
import com.data.biz.domain.WindPoint;
import com.data.biz.service.IBizSensorCommonService;
import com.data.common.annotation.Log;
import com.data.common.constant.UserConstants;
import com.data.common.core.controller.BaseController;
import com.data.common.core.domain.AjaxResult;
import com.data.common.core.page.TableDataInfo;
import com.data.common.enums.BusinessType;
import com.data.common.utils.poi.ExcelUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/system/fanlist")
public class SystemFanController extends BaseController {
    private String prefix = "system/fanlist";

    @Autowired
    private IBizSensorCommonService commonService;

    @RequiresPermissions("system:fanlist:view")
    @GetMapping()
    public String operlog()
    {
        return prefix + "/fanlist";
    }

    @RequiresPermissions("system:fanlist:fanlist")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BizFan fan)
    {
        startPage();
        List<BizFan> list = commonService.getFanlist(fan);
        return getDataTable(list);
    }

    @Log(title = "风机管理", businessType = BusinessType.EXPORT)
    @RequiresPermissions("system:fanlist:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BizFan fan)
    {
        List<BizFan> list = commonService.getFanlist(fan);
        ExcelUtil<BizFan> util = new ExcelUtil<BizFan>(BizFan.class);
        return util.exportExcel(list, "风机数据");
    }

    @RequiresPermissions("system:fanlist:remove")
    @Log(title = "风机管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        try
        {
            return toAjax(commonService.deleteFanByIds(ids));
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }

    /**
     * 新增风机
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存风机
     */
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated BizFan fan) throws Exception {

        if(fan.getFanName() == null){
            return error("风机名称不能为空");
        }
        if (UserConstants.FAN_NAME_NOT_UNIQUE.equals(commonService.checkFanNameUnique(fan)))
        {
            return error("新增风机'" + fan.getFanName() + "'失败，风机名称已存在");
        }
        return toAjax(commonService.insertFan(fan));
    }

    /**
     * 修改风机
     */
    @GetMapping("/edit/{fanId}")
    public String edit(@PathVariable("fanId") Integer fanId, ModelMap mmap)
    {
        mmap.put("fan", commonService.selectFanById(fanId));
        return prefix + "/edit";
    }


    /**
     * 修改保存风机
     */
    @RequiresPermissions("system:fanlist:edit")
    @Log(title = "风机管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated BizFan fan)
    {
        if(fan.getFanName() == null){
            return error("风机名称不能为空");
        }
        if (UserConstants.FAN_NAME_NOT_UNIQUE.equals(commonService.checkFanNameUnique(fan)))
        {
            return error("修改风机'" + fan.getFanName() + "'失败，风机名称已存在");
        }
        return toAjax(commonService.updateFan(fan));
    }


    /**
     * 校验风机编码
     */
    @PostMapping("/checkFanNameUnique")
    @ResponseBody
    public String checkFanNameUnique(BizFan fan)
    {
        return commonService.checkFanNameUnique(fan);
    }


}
